package com.neusoft.my12306.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.my12306.common.res.CommonResult;
import com.neusoft.my12306.entity.Schedule;
import com.neusoft.my12306.mapper.ScheduleMapper;
import com.neusoft.my12306.service.IScheduleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 段静迪
 * @since 2023-07-04
 */
@Service
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule> implements IScheduleService {

    @Override
    public CommonResult findAll() {
        List<Schedule> lss =  baseMapper.selectList(null);
        CommonResult result = new CommonResult(lss);
        if (lss ==null || lss.size() ==0)
            result.setNotFound();
        return result;
    }

    @Override
    public CommonResult findByTrainno(String trainno) {
        QueryWrapper  queryWrapper = new QueryWrapper();
        queryWrapper.eq("trainno" , trainno);
        List<Schedule> lss =  baseMapper.selectList(queryWrapper);
        CommonResult result = new CommonResult(lss);
        if (lss ==null || lss.size() ==0)
            result.setNotFound();
        return result;
    }

    @Override
    public CommonResult addSchdeule(Schedule schedule) {
// 只有ID的属性值为null时，mybatis-plus才启用@TableId(value = "sid", type = IdType.AUTO) IdType.AUTO
        schedule.setSid(null);
        int i =  baseMapper.insert(schedule);
        CommonResult  result =new CommonResult( schedule.getSid());
        if (i <= 0 ){
            result.setNotModified();
        }


        return result;
    }

    @Override
    public CommonResult delSchdeule(String sid) {

        int i =  baseMapper.deleteById(sid);
        CommonResult  result =new CommonResult(  i );
        if (i <= 0 ){
            result.setNotModified();
        }
        return result;
    }

    @Override
    public CommonResult modSchdeule(Schedule schedule) {
        int i  =  baseMapper.updateById(schedule);
        CommonResult  result =new CommonResult(  i );
        if (i <= 0 ){
            result.setNotModified();
        }
        return result;
    }
}
